<template>
  <!-- 1.1input事件发送 -->
  <input
    type="text"
    class="myinput"
    @input="handlerInput"
    :class="{ success: statu, error: !statu }"
    @change="handlerChange"
  />
</template>

<script>
export default {
  props: {
    rules: {
      type: RegExp,
    },
    msg: {
      type: String,
      default: "当前输入有误",
    },
  },
  data() {
    return {
      statu: true,
    };
  },
  methods: {
    handlerInput(e) {
      if (this.rules && this.rules.test(e.target.value)) {
        this.statu = true;
      } else {
        this.statu = false;
      }
      //1.2 input事件发送
      this.$emit("input", e.target.value);
    },
    handlerChange(e) {
      if (this.rules && !this.rules.test(e.target.value)) {
        // alert(this.msg);
        this.$toast.fail({
          message: this.msg,
          duration: 5000,
        });
      }
    },
  },
};
</script>

<style lang="less" scoped>
.myinput {
  width: 320/360 * 100vw;
  height: 60px;
  outline: none;
  border: none;
  border-bottom: 3px solid #ccc;
  font-size: 20px;
  line-height: 60px;
}
.success {
  border-bottom-color: green;
}
.error {
  border-bottom-color: red;
}
</style>